/*
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2022-03-22 12:12:49
 * @LastEditTime: 2022-10-28 16:40:12
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description: 标签页
 * @FilePath: \vue3-element-plus-web\src\store\modules\tagsView.js
 * @Copyright 版权声明
 */
import { setStore, getStore/**, removeStore */ } from '@/plugins/store'
const tagsView = {
  state: {
    visitedViews: getStore({
      name: 'tagList'
    }) || [],
    cachedViews: []
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      if (state.visitedViews.some((v) => v.path === view.path)) return
      state.visitedViews.push(
        Object.assign({}, view, {
          title: view.meta.title || 'no-name'
        })
      )
      if (!view.meta.noCache) {
        state.cachedViews.push(view.name)
      }
      setStore({ name: 'tagList', content: state.visitedViews, type: 'session' })
    },
    DEL_VISITED_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
      setStore({ name: 'tagList', content: state.visitedViews, type: 'session' })
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      const visitedViewsTemp = []
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path || v.path === '/home' || v.path === '/') {
          visitedViewsTemp.push(state.visitedViews[i])
          // state.visitedViews = state.visitedViews.slice(i, i + 1)
          // break
        }
      }
      state.visitedViews = visitedViewsTemp

      const cachedViewsTemp = []
      for (const i of state.cachedViews) {
        if (i === view.name || i === 'home') {
          const index = state.cachedViews.indexOf(i)
          cachedViewsTemp.push(state.cachedViews[index])
          // state.cachedViews = state.cachedViews.slice(index, index + 1)
          // break
        }
      }
      state.cachedViews = cachedViewsTemp
      setStore({ name: 'tagList', content: state.visitedViews, type: 'session' })
    },
    DEL_ALL_VIEWS: (state) => {
      // 新代码：关闭所有（不包括主页）
      const visitedViewsTemp = []
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === '/home' || v.path === '/') {
          visitedViewsTemp.push(state.visitedViews[i])
        }
      }
      state.visitedViews = visitedViewsTemp
      const cachedViewsTemp = []
      for (const i of state.cachedViews) {
        if (i === 'home') {
          const index = state.cachedViews.indexOf(i)
          cachedViewsTemp.push(state.cachedViews[index])
        }
      }
      state.cachedViews = cachedViewsTemp
      setStore({ name: 'tagList', content: state.visitedViews, type: 'session' })
      // 老代码：关闭所有（包括主页）
      // state.visitedViews = []
      // state.cachedViews = []
      // setStore({ name: 'tagList', content: state.visitedViews, type: 'session' })
    }
  },
  actions: {
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews({ commit, state }) {
      return new Promise((resolve) => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    }
  }
}

export default tagsView
